﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;

namespace RealtorsPortal
{
    public partial class Detail : System.Web.UI.Page
    {
        string id = "";
        AdvertisementsDAL advd = new AdvertisementsDAL();
        AdsImagesDAL aid = new AdsImagesDAL();
        AdvCommentDAL acd = new AdvCommentDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["AdvId"] != null)
                {
                    id = Request.QueryString["AdvId"].ToString();
                    this.LoadAndBindData();
                }
                else
                {
                    PropertyDetailPanel.Visible = false;
                }
            }
        }
        private void LoadAndBindData()
        {
            DataTable dtAdv = advd.GetAdsByAdvId(id);
            if (dtAdv.Rows.Count > 0) {
                rptDetail2.DataSource = rptPropertyDetail.DataSource = dtAdv;
                rptPropertyDetail.DataBind();
                rptDetail2.DataBind();
                rptSlideShow.DataSource = aid.GetAdsImagesByAdvId(id);
                rptSlideShow.DataBind();

                lvwComments.DataSource = acd.GetCommentsByAdvIDDataTable(id);
                lvwComments.DataBind();

            }
            else
            {
                PropertyDetailPanel.Visible = false;
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            lblContactStatus.Text = "Send email successful";
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            AdvComment c = new AdvComment();
            string advId = Request.QueryString["AdvId"].ToString();
            c.AdvId = int.Parse(advId);
            c.Content = txtContent.Text;
            c.FullName = txtFullName.Text;
            c.WritedDate = DateTime.Now.ToString();
            if (acd.InsertComment(c))
            {
                lblCommentStatus.Text = "Post comment successful";
                lvwComments.DataSource = acd.GetCommentsByAdvIDDataTable(advId);
                lvwComments.DataBind();
            }
        }
    }
}